﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestServerConsoleApp
{
    class Program
    {
        static Socket socket;

        static byte[] buffer;


        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            buffer = new byte[1024];

            Task.Run((Action)delegate
            {
                while (true)
                {
                    if (socket != null)
                    {

                        Socket newSocket = socket.Accept();


                        Task.Run((Action)delegate
                        {
                            while(true)
                            {
                                int byteCount = newSocket.Receive(buffer);

                                if (byteCount > 0)
                                    Console.WriteLine(Encoding.UTF8.GetString(buffer));

                                Thread.Sleep(10);
                            }
                

                        });

                        Thread.Sleep(10);
                    }

                    Thread.Sleep(1000);

                }


            });

            socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
            var ePoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 40000);
            socket.Bind(ePoint);
            socket.Listen();


            Console.ReadLine();
        }
    }
}
